import pygame
from game import Game


class Monster:
    def __init__(self, left: int, top: int, speed: int):
        self.monster_images = {
            "right": pygame.image.load("Img/怪物右.png"),
            "left": pygame.image.load("Img/怪物左.png"),
        }
        self.monster_image = self.monster_images["right"]
        self.Monster_rect = self.monster_image.get_rect()
        self.Monster_rect.left = left
        self.Monster_rect.top = top
        self.Monster_speed = speed
        self.Monster_fangxiang = True
        self.Monster_fangxiang2 = True
        self.Monster_fangxiang3 = True
        self.Monster_fangxiang4 = True
        self.Monster_fangxiang5 = True
        self.Monster_fangxiang6 = True

    def monster_display(self):
        for index, monster in enumerate(Game.monster_list):
            monster_x = monster["x"]
            monster_y = monster["y"]

            # 第一个怪兽
            if index == 0 and self.Monster_fangxiang:
                monster["x"] += self.Monster_speed
                Game.window.blit(self.monster_images["right"], (monster_x, monster_y))
                if monster["x"] == Game.monster1_end:
                    self.Monster_fangxiang = False

            elif index == 0 and not self.Monster_fangxiang:
                monster["x"] -= self.Monster_speed
                Game.window.blit(self.monster_images["left"], (monster_x, monster_y))
                if monster["x"] == Game.monster1_star:
                    self.Monster_fangxiang = True

            # 第二个怪兽
            if index == 1 and self.Monster_fangxiang2:
                monster["x"] += self.Monster_speed
                Game.window.blit(self.monster_images["right"], (monster_x, monster_y))
                if monster["x"] == Game.monster2_end:
                    self.Monster_fangxiang2 = False

            elif index == 1 and not self.Monster_fangxiang2:
                monster["x"] -= self.Monster_speed
                Game.window.blit(self.monster_images["left"], (monster_x, monster_y))
                if monster["x"] == Game.monster2_star:
                    self.Monster_fangxiang2 = True

            # 第三个怪兽
            if index == 2 and self.Monster_fangxiang3:
                monster["x"] += self.Monster_speed
                Game.window.blit(self.monster_images["right"], (monster_x, monster_y))
                if monster["x"] == Game.monster3_end:
                    self.Monster_fangxiang3 = False

            elif index == 2 and not self.Monster_fangxiang3:
                monster["x"] -= self.Monster_speed
                Game.window.blit(self.monster_images["left"], (monster_x, monster_y))
                if monster["x"] == Game.monster3_star:
                    self.Monster_fangxiang3 = True

            # 第三个怪兽
            if index == 3 and self.Monster_fangxiang4:
                monster["x"] += self.Monster_speed
                Game.window.blit(self.monster_images["right"], (monster_x, monster_y))
                if monster["x"] == Game.monster4_end:
                    self.Monster_fangxiang4 = False

            elif index == 3 and not self.Monster_fangxiang4:
                monster["x"] -= self.Monster_speed
                Game.window.blit(self.monster_images["left"], (monster_x, monster_y))
                if monster["x"] == Game.monster4_star:
                    self.Monster_fangxiang4 = True

            # 第三个怪兽
            if index == 4 and self.Monster_fangxiang5:
                monster["x"] += self.Monster_speed
                Game.window.blit(self.monster_images["right"], (monster_x, monster_y))
                if monster["x"] == Game.monster5_end:
                    self.Monster_fangxiang5 = False
            elif index == 4 and not self.Monster_fangxiang5:
                monster["x"] -= self.Monster_speed
                Game.window.blit(self.monster_images["left"], (monster_x, monster_y))
                if monster["x"] == Game.monster5_star:
                    self.Monster_fangxiang5 = True

            # 第三个怪兽
            if index == 5 and self.Monster_fangxiang6:
                monster["x"] += self.Monster_speed
                Game.window.blit(self.monster_images["right"], (monster_x, monster_y))
                if monster["x"] == Game.monster6_end:
                    self.Monster_fangxiang6 = False
            elif index == 5 and not self.Monster_fangxiang6:
                monster["x"] -= self.Monster_speed
                Game.window.blit(self.monster_images["left"], (monster_x, monster_y))
                if monster["x"] == Game.monster6_star:
                    self.Monster_fangxiang6 = True

        for monster in Game.monster_list:
            monster_rect = self.monster_image.get_rect()
            monster_rect.x = monster["x"]
            monster_rect.y = monster["y"]

            for bullet in Game.bullet_list:
                if bullet.Bullet_rect.colliderect(monster_rect):
                    # 碰撞发生，从宝石列表中删除宝石
                    Game.guaiwusi = pygame.mixer.Sound("wav/怪物死.wav")
                    Game.guaiwusi.play()
                    monster["x"] = -10000000
                    Game.bullet_list.remove(bullet)
                    gem_music = pygame.mixer.Sound("wav/碰撞.wav")
                    gem_music.play()


